/*
 * TERMS OF USE - SCROLL TO
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2011 Artur Figueira
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
 */

(function( $ ){
  $.fn.scrollTo = function( opts ) {
	  var settings = {
			  'position': 0,
			  'speed': 500
	  };
	  
	  var methods = {
		'OptionsParser' : function( opts ){
			this._opts = opts;
			
			this.parse = function(){
				var parsedValue = this.parseElement(this._opts.element);
				var position = this.parsePosition((parsedValue) ? parsedValue : this._opts.position);
				var speed =  this.parseSpeed(this._opts.speed);
				var output = { 
					'position' : (position) ? position : settings.position,
					'speed' : (speed) ? speed : settings.speed
				  };
				
			  return output;
			}
			
			this.parseElement = function( element ){
				if(element == null){
					return;
				}
				
				var jElement = element;
				if(typeof(element)=='string'){
					elsArray = $(document).find(element);
					if(elsArray && elsArray[0]){
						jElement = $(elsArray[0]);
					}
				}
				return jElement.offset().top;
			}
			
			this.parsePosition = function( position ){
				if(/(px)$/.test(position)){
					position = position.substr(0, position.length-2); 
				}
				return parseInt(position);
			}
			
			this.parseSpeed = function( speed ){
				if(speed != null && ((speed < 0) || 
						(typeof(speed)=='string' && (speed != 'slow' || speed != 'fast')))){
					speed = null;
				}
				return speed;
			}
		}
	  }
	  
	  if(opts){
		  var optionsParser = new methods.OptionsParser(opts);
		  $.extend(settings, optionsParser.parse());
	  }
	  
	  this.click(function(){
		  $('html, body').animate({scrollTop: settings.position+'px'}, settings.speed);
		  return false;
	  });
	  
	  return false;
  };
})( jQuery );